In [23]:
import leafmap
import geopandas as gpd
import rasterio
from rasterio.plot import show
import matplotlib.pyplot as plt
In [24]:
tif_file = 'C:/Users/HP/Desktop/rasters/multiespectral.tif'
Geopandas¶
In [25]:
#geojson_file = 'map.geojson'
geojson_file = 'mapBosque.geojson'
gdf = gpd.read_file(geojson_file)
gdf
gdf.plot()
plt.title("Límites del archivo GeoJSON")
plt.show()
Rasterio¶
In [26]:
with rasterio.open(tif_file) as src:
# Mostrar las bandas disponibles en la imagen
print(f"Bandas disponibles: {src.count}")
# Leer las bandas (por ejemplo, banda 4 (roja) y banda 5 (infrarroja))
red_band = src.read(4)
nir_band = src.read(5)
# Visualizar las bandas usando Rasterio
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
# Mostrar la banda roja
ax[0].imshow(red_band, cmap='Reds')
ax[0].set_title("Banda Roja (Banda 4)")
ax[0].axis('off')
# Mostrar la banda infrarroja
ax[1].imshow(nir_band, cmap='Blues')
ax[1].set_title("Banda Infrarroja (Banda 5)")
ax[1].axis('off')
plt.show()
Bandas disponibles: 6
Rioxarray¶
In [27]:
import rioxarray
# Cargar una imagen multiespectral (GeoTIFF) con rioxarray
img = rioxarray.open_rasterio(tif_file)
# Ver las bandas disponibles
print(img)
# Obtener una banda específica (por ejemplo, la banda verde)
green_band = img.sel(band=1)
# Mostrar la banda
green_band.plot()
<xarray.DataArray (band: 6, y: 3722, x: 4797)> Size: 214MB
[107126604 values with dtype=uint16]
Coordinates:
* band (band) int32 24B 1 2 3 4 5 6
* x (x) float64 38kB -79.97 -79.97 -79.97 ... -79.97 -79.97 -79.97
* y (y) float64 30kB -2.142 -2.142 -2.142 ... -2.144 -2.144 -2.144
spatial_ref int32 4B 0
Attributes:
AREA_OR_POINT: Area
scale_factor: 1.0
add_offset: 0.0
units: ('metre', 'metre', 'metre', 'metre', 'metre', 'metre')
Out[27]:
<matplotlib.collections.QuadMesh at 0x1f859872830>
MapLibre¶
In [28]:
import folium
import rasterio
from folium.raster_layers import ImageOverlay
import numpy as np
img_rgb = 'C:/Users/Dell/Desktop/rasters/rgb.tif'
lat = -2.1455209570360694
lon = -79.9776077614871
m = folium.Map(location=[lat, lon], zoom_start=14,
tiles='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
attr='© OpenStreetMap contributors')
with rasterio.open(tif_file) as src:
red_band = src.read(1)
green_band = src.read(2)
blue_band = src.read(3)
red_band = (red_band - red_band.min()) / (red_band.max() - red_band.min()) * 255
green_band = (green_band - green_band.min()) / (green_band.max() - green_band.min()) * 255
blue_band = (blue_band - blue_band.min()) / (blue_band.max() - blue_band.min()) * 255
red_band = np.clip(red_band, 0, 255).astype(np.uint8)
green_band = np.clip(green_band, 0, 255).astype(np.uint8)
blue_band = np.clip(blue_band, 0, 255).astype(np.uint8)
rgb_image = np.stack((red_band, green_band, blue_band), axis=-1)
bounds = src.bounds
img_overlay = ImageOverlay(
image=rgb_image,
bounds=[(bounds[1], bounds[0]), (bounds[3], bounds[2])],
opacity=0.7,
)
img_overlay.add_to(m)
folium.GeoJson(geojson_file).add_to(m)
#m.save("mapa_bosque_prosperina_con_imagen.html")
m
Out[28]:
Make this Notebook Trusted to load map: File -> Trust Notebook